package com.zhaungjie.base.utils.PropertiesCopyUtils;

import com.zhaungjie.base.utils.LambdaStructureParser.LambdaStructureParser;
import com.zhaungjie.base.utils.PropertiesCopyUtils.operation.AssociatedCopyOperation;
import com.zhaungjie.base.utils.PropertiesCopyUtils.operation.SimplePropertiesCopyOperation;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.function.Consumer;

@Getter
@Setter
public class PropertiesCopyConfigRoot<S,T> {
    private Class<S> sourceClazz;
    private Class<T> targetClazz;

    private T template;

    public PropertiesCopyConfigRoot(Class<S> sourceClazz, Class<T> targetClazz) {
        this.sourceClazz = sourceClazz;
        this.targetClazz = targetClazz;
    }

    /**
     * 配置后再进行拷贝
     *
     * @param propertiesCopyConfigConsumer 属性拷贝配置消费者
     * @return {@link SimplePropertiesCopyOperation}
     */
    public SimplePropertiesCopyOperation<S,T> config(Consumer<PropertiesCopyConfig<S,T>> propertiesCopyConfigConsumer) {
        PropertiesCopyConfig<S,T> propertiesCopyConfig = new PropertiesCopyConfig();
        if (propertiesCopyConfigConsumer != null) propertiesCopyConfigConsumer.accept(propertiesCopyConfig);
        return new SimplePropertiesCopyOperation<>(propertiesCopyConfig,this);
    }


    /**
     * 无配置
     *
     * @return {@link SimplePropertiesCopyOperation}
     */
    public T produce(S[] sources) {
        return genSimplePropertiesCopyOperation().produce(sources);
    }

    /**
     * 生成SimplePropertiesCopyOperation对象
     *
     * @return {@link SimplePropertiesCopyOperation}<{@link S}, {@link T}>
     */
    private SimplePropertiesCopyOperation<S, T> genSimplePropertiesCopyOperation() {
        return  new SimplePropertiesCopyOperation(new PropertiesCopyConfig<S, T>(), this);
    }

    /**
     * 单拷贝模板设置
     * @param template
     * @return
     */
    public PropertiesCopyConfigRoot<S,T> setTemplate(T template) {
        this.template = template;
        return this;
    }



    /**
     * 不配置的单拷贝
     * @param source
     * @return
     */
    public T produce(S source) {
        return genSimplePropertiesCopyOperation().produce(source);
    }

    /**
     * 不配置的批量拷贝
     *
     * @param sources 来源
     * @return {@link List}<{@link T}>
     */
    public List<T> batchProduce(List<S> sources) {
        if (template != null) {
            throw new RuntimeException("批量拷贝暂不支持模板");
        }
       return genSimplePropertiesCopyOperation().batchProduce(sources);
    }

    /**
     * 关联拷贝-方向
     *
     * @param sourceSFunction 源函数
     * @param targetSFunction 目标函数
     * @return {@link AssociatedCopyOperation}<{@link S},{@link T}>
     */
    public AssociatedCopyOperation<S,T> associated(LambdaStructureParser.NANRFunction<S> sourceSFunction, LambdaStructureParser.NANRFunction<T> targetSFunction) {
        return genSimplePropertiesCopyOperation().associated(sourceSFunction,targetSFunction);
    }


}
